package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.city_home_app.Activity.MainActivity;
import com.example.city_home_app.Activity.PhotoViewActivity;
import com.example.city_home_app.Bean.Down_Moment;
import com.example.city_home_app.Bean.MomentImg;
import com.example.city_home_app.Fragment.Fragment_moment;
import com.example.city_home_app.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_MomentImg extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<MomentImg> list;
    private ArrayList<String> imgurls = new ArrayList<>();

    public Adapter_MomentImg(Context context) {
        this.context = context;
    }

    public void setList(List<MomentImg> list) {
        this.list = list;
        getimgurls();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_moment_img, parent, false);
        Adapter_MomentImg.ViewHolder viewHolder = new Adapter_MomentImg.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final MomentImg bean = list.get(position);
        if (bean == null||bean.getUrl().equals("")) {
            return;
        }
        final Adapter_MomentImg.ViewHolder viewHolder = (Adapter_MomentImg.ViewHolder) holder;
        if(bean.getType()==1){
            viewHolder.imgRecitemMomentimgVideo.setVisibility(View.GONE);
            Picasso.with(context)
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getUrl())
                    .into(viewHolder.imgRecitemMomentimgImg);
        }else{
            viewHolder.imgRecitemMomentimgVideo.setVisibility(View.VISIBLE);
            loadingVideoImage(viewHolder.imgRecitemMomentimgImg,bean.getUrl());
        }


        //查看大图
        viewHolder.imgRecitemMomentimgImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoViewActivity.class);
                intent.putStringArrayListExtra("urls",imgurls);
                intent.putExtra("currentPosition", position);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_momentimg_img)
        ImageView imgRecitemMomentimgImg;
        @BindView(R.id.img_recitem_momentimg_video)
        ImageView imgRecitemMomentimgVideo;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private static Bitmap getVideoThumb(String path) {

        MediaMetadataRetriever media = new MediaMetadataRetriever();

        media.setDataSource("http://siminzhang.top:8088/city_home_server/public/static/videos/"+path, new HashMap());

        return media.getFrameAtTime();
    }

    private void loadingVideoImage(final ImageView imageView, final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap = getVideoThumb(url);
                imageView.post(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();
    }

    private void getimgurls(){
        for(MomentImg bean: list){
            if(bean.getType()==1){
                imgurls.add(bean.getUrl());
            }
        }
    }

}
